package com.cn.yugan.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cn.yugan.Engine.Http.AddReqCookie;
import com.cn.yugan.Engine.Http.Nocation;
import com.cn.yugan.Engine.Json.NoticeMessageJsonObj;
import com.cn.yugan.Engine.Json.NoticeNormalMessageJsonObj;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.mobile.R;
import com.loopj.android.http.AsyncHttpClient;


public class InnerMessageActivity extends AppCompatActivity {
    private AsyncHttpClient asyncHttpClient;
    private ListView mlistview;
    private String[] arr = {"回复我的", "@我的", "私信我的"};
    private int[] ndata = new int[3];

    private NoticeNormalMessageJsonObj mdata1 = new NoticeNormalMessageJsonObj();
    private NoticeNormalMessageJsonObj mdata2 = new NoticeNormalMessageJsonObj();
    private NoticeMessageJsonObj mdata3 = new NoticeMessageJsonObj();

    private InnerMessageListAdapter madapter;
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Nocation.NOTICE_ATME:
                    ndata[1] = ((NoticeNormalMessageJsonObj) (msg.obj)).getUnread();
                    mdata1 = (NoticeNormalMessageJsonObj) msg.obj;
                    madapter.setdata(ndata);
                    madapter.notifyDataSetChanged();
                    break;
                case Nocation.NOTICE_REPLYME:
                    ndata[0] = ((NoticeNormalMessageJsonObj) (msg.obj)).getUnread();
                    mdata2 = (NoticeNormalMessageJsonObj) msg.obj;
                    madapter.setdata(ndata);
                    madapter.notifyDataSetChanged();
                    break;
                case Nocation.NOTICE_MESSAGE:
                    ndata[2] = ((NoticeMessageJsonObj) (msg.obj)).getUnread();
                    mdata3 = (NoticeMessageJsonObj) msg.obj;
                    madapter.setdata(ndata);
                    madapter.notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_message_list);

        initactionbar();
        initview();
        initdata();

    }

    private void initdata() {
        asyncHttpClient = new AsyncHttpClient(true, 80, 443);
        asyncHttpClient.addHeader("cookie", AddReqCookie.getNormalheader(this));
        Nocation.getnotice(this, Nocation.NOTICE_ATME, asyncHttpClient, mhandler);
        Nocation.getnotice(this, Nocation.NOTICE_REPLYME, asyncHttpClient, mhandler);
        Nocation.getnotice(this, Nocation.NOTICE_MESSAGE, asyncHttpClient, mhandler);

    }

    private void initview() {
        mlistview = (ListView) findViewById(R.id.inner_message_list_lv);
        madapter = new InnerMessageListAdapter();
        mlistview.setAdapter(madapter);
        mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Nocation.clearnotice(InnerMessageActivity.this, Nocation.NOTICE_REPLYME, asyncHttpClient);
                        Intent intent = new Intent(InnerMessageActivity.this, InnerMessageInfoListActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("data", mdata2);
                        LogUtils.e(mdata2.getData().toString());
                        LogUtils.e(mdata2.getData().get(0).getTitle());
                        intent.putExtras(bundle);
                        intent.putExtra("type", InnerMessageInfoListActivity.MODE_REPLAYME);
                        startActivity(intent);
                        break;
                    case 1:
                        Nocation.clearnotice(InnerMessageActivity.this, Nocation.NOTICE_ATME, asyncHttpClient);
                        Intent intent1 = new Intent(InnerMessageActivity.this, InnerMessageInfoListActivity.class);
                        Bundle bundle1 = new Bundle();
                        bundle1.putSerializable("data", mdata1);
                        LogUtils.e(mdata1.getData().toString());
                        LogUtils.e(mdata1.getData().get(0).getTitle());
                        intent1.putExtras(bundle1);
                        intent1.putExtra("type", InnerMessageInfoListActivity.MODE_ATME);
                        startActivity(intent1);
                        break;
                    case 2:
                        Nocation.clearnotice(InnerMessageActivity.this, Nocation.NOTICE_MESSAGE, asyncHttpClient);
                        Intent intent2 = new Intent(InnerMessageActivity.this, InnerMessageInfoListActivity.class);
                        Bundle bundle2 = new Bundle();
                        bundle2.putSerializable("data", mdata3);
                        LogUtils.e(mdata3.getData().toString());
                        LogUtils.e(mdata3.getData().get(0).getUsername());
                        intent2.putExtras(bundle2);
                        intent2.putExtra("type", InnerMessageInfoListActivity.MODE_MESSAGE);
                        startActivity(intent2);
                        break;
                }

            }
        });
    }


    private void initactionbar() {
        ActionBar actionbar = getSupportActionBar();
        actionbar.setTitle("消息中心");
    }

    private class InnerMessageListAdapter extends BaseAdapter {
        private ImageView mimageview;
        private TextView mTextview1;
        private TextView mTextview2;
        private int[] nmessage = {0, 0, 0};

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(InnerMessageActivity.this, R.layout.item_settings_normal, null);
            mimageview = (ImageView) convertView.findViewById(R.id.setting_iv);
            mTextview1 = (TextView) convertView.findViewById(R.id.setting_title);
            mTextview2 = (TextView) convertView.findViewById(R.id.setting_notification_red);

            switch (position) {
                case 0:
                    mTextview1.setText(arr[0]);
                    mimageview.setImageResource(R.mipmap.ic_notification_reply);
                    if (nmessage[0] > 0) {
                        mTextview2.setText(nmessage[0] + "");
                    } else {
                        mTextview2.setVisibility(View.INVISIBLE);
                    }
                    break;

                case 1:
                    mTextview1.setText(arr[1]);
                    mimageview.setImageResource(R.mipmap.ic_notification_at);
                    if (nmessage[1] > 0) {
                        mTextview2.setText(nmessage[1] + "");
                    } else {
                        mTextview2.setVisibility(View.INVISIBLE);
                    }
                    break;

                case 2:
                    mTextview1.setText(arr[2]);
                    mimageview.setImageResource(R.mipmap.ic_notification_sys);
                    if (nmessage[2] > 0) {
                        mTextview2.setText(nmessage[2] + "");
                    } else {
                        mTextview2.setVisibility(View.INVISIBLE);
                    }
                    break;

            }
            return convertView;
        }

        public void setdata(int[] nmessage) {
            this.nmessage = nmessage;
        }
    }

    @Override
    protected void onResume() {
        initdata();
        super.onResume();
    }
}